<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\CouponService as Coupon;
use Illuminate\Http\Request;

class CouponController extends BaseController
{
    //会员劵

    public function __construct()
    {
        $this->db = new Coupon();
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $is_use = $request->post('is_use', 0); //可用，2失效
        $res = $this->db->getList($pages, $uid, $is_use);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //领取
    public function apply(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->apply($uid, $id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //获取
    public function getOne(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $amount = $request->post('amount', 0);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        if (!$amount) {
            return $this->toJson(-2, '总价必须的');
        }
        $res = $this->db->getOne($uid, $amount);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //获取选项列表
    public function getOption(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $amount = $request->post('amount', 0);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        if (!$amount) {
            return $this->toJson(-2, '总价必须的');
        }
        $res = $this->db->getOption($uid, $amount);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //获取新用户优惠劵列表
    public function getNewUserList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        $res = $this->db->getNewUserList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //领取新用户优惠劵
    public function addNewUser(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        $res = $this->db->addNewUser($uid, $id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //开关
    public function getSwitchOne(Request $request)
    {
//        $token = $request->post('token', '');
//        $uid = $this->db->addApiCount($token);
//        $id = $request->post('id', 0);
        $filepath = 'weixin.json';
        $d = file_get_contents($filepath);
        if (!$d) {
            return $this->toJson(-2, '没有配置');
        }
        $d = json_decode($d, true);
        $resData = [
            'is_switch' => $d['coupon_switch'],
        ];
        return $this->toJson(0, '', $resData);
    }
}